<?php
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Smarty.class.php");					//--> out template
include_once("Product.php");						//--> Merchant
include_once("Operation.php");						//--> Operation
include_once("TaxRates.php");						//-->
include_once("UploadFiles.php");					//--> UploadFiles
include_once("Files.php");							//--> Files
include_once("Attributes.php");						//--> Attributes
require_once("controlHeader.php");					//--> system control header

$objOperate = new Operation();						//--> Operation
$objProduct	= new Product(DB_TAG_SYSTEM, $uid);		//--> Product
$objTaxRates= new TaxRates(DB_TAG_PUBLIC, $uid);	//--> TaxRates
$objFiles	= new Files(DB_TAG_PUBLIC, $uid);		//--> Files
$objUploadFiles	= new UploadFiles(DB_TAG_PUBLIC, $uid);				//--> UploadFiles
$objAttributes	= new Attributes(DB_TAG_PUBLIC, $uid);			//--> Attributes

$error_message 	= '';

$arrAllImage 	= array();

$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);


$arrInfo = $objProduct->getFromID($arrOperate, $id, $LANGEUAGE_ID);

if(is_array($arrInfo)){
	$arrTaxRate = $objTaxRates->getTaxRate($arrOperate, $arrInfo['taxRateID']);
	$arrInfo['taxRate'] = $arrTaxRate['summary'];
	
	$arrManufacturer = $objTaxRates->getHash(DB_TAG_PUBLIC, "SELECT id, name FROM manufacturers WHERE id = " . $arrInfo['manufacturerID']);
	
	$arrInfo['manufacturer'] = $arrManufacturer[$arrInfo['manufacturerID']];

//	$sizeValue 	= $objProduct->getAllData(DB_TAG_PUBLIC, "SELECT a.name FROM attributes a, products_attributes pa WHERE a.id = pa.attributeID AND pa.productID = $id");
	
//	$arrImages = $objTaxRates->getHash(DB_TAG_PUBLIC, "SELECT u.id, u.extName FROM upload_files u, product_images p WHERE u.id = p.imageID AND p.productID = $id AND type = " . UploadFiles::TYPE_IMAGE);
	
	$sql 		= "SELECT p.imageID, p.attributeID, u.extName FROM product_images p, upload_files u WHERE p.imageID = u.id AND p.productID = $id";
	$arrImages 	= $objProduct->getAllData(DB_TAG_PUBLIC, $sql);
	
	foreach($arrImages as $image){
		$arrtmp = array();
		
		if(array_key_exists($image['attributeID'], $arrAllImage)){
			$arrSub 		= $arrAllImage[$image['attributeID']];
			$arrImageIDs 	= $arrSub['imageIDs'];
			
			$arrImageIDs[$image['imageID']]['imageID'] 	= $image['imageID'];
			$arrImageIDs[$image['imageID']]['imageExt'] 	= $image['extName'];
			
			$arrSub['imageIDs'] = $arrImageIDs;
			
		}else{
			$arrSub 		= array();
			$arrImageIDs 	= array();
			
			$arrImageIDs[$image['imageID']]['imageID'] 	= $image['imageID'];
			$arrImageIDs[$image['imageID']]['imageExt'] 	= $image['extName'];
			
			if($image['attributeID'] != '0')
				$arrSub['attributeName'] = $objProduct->getColumn(DB_TAG_PUBLIC, "SELECT name FROM attributes WHERE id = " . $image['attributeID']);
			else
				$arrSub['attributeName'] = 'Unknow Attributes';
			
			$arrSub['imageIDs'] = $arrImageIDs;
			
			
		}
		
		$arrAllImage[$image['attributeID']] = $arrSub;
	}
}

/*----- load attributes ------*/
$attributePrice	= array();
$attributes		= array();
$sortsValue		= array();

$sizeValuess 	= $objProduct->getAllData(DB_TAG_PUBLIC, "SELECT pa.attributeID, a.name, a.parentid, pa.attributePrice, pa.pricePrefix FROM attributes a, products_attributes pa WHERE a.parentid != 33 AND a.id = pa.attributeID AND pa.productID = $id ORDER BY a.sorts");
foreach($sizeValuess as $arrRow){
	if(!empty($arrRow['parentid'])){
		if($arrRow['attributePrice'] > 0)
			$attributePrice[$arrRow['attributeID']] = $arrRow['attributePrice'];
		
		$attribute = $objProduct->getRow(DB_TAG_PUBLIC, "SELECT name, sorts FROM attributes WHERE id = " . $arrRow['parentid']);
		
		if(array_key_exists($attribute['name'], $attributes)){
			$arrSub 	= $attributes[$attribute['name']];
			$arrData 	= $arrSub['attri'];
			
			$key = $arrRow['attributeID']; //attribute value
			
			if($arrRow['attributePrice'] > 0)
				$arrData[$key] = $arrRow['name'] . ' ($' . $arrRow['pricePrefix'] . ($arrRow['attributePrice'] / 100) . ')';
			else
				$arrData[$key] = $arrRow['name'];
			
			$arrSub['attri'] = $arrData;
			
			$attributes[$attribute['name']] = $arrSub;
		}else{
			$arrData 			= array();
			$arrOption 			= array();
			
			$arrSub 			= array();
			
			$key = $arrRow['attributeID']; //attribute value
			
			if($arrRow['attributePrice'] > 0){
				$arrData[$key] = $arrRow['name'] . ' ($' . $arrRow['pricePrefix'] . ($arrRow['attributePrice'] / 100) . ')';
			}else
				$arrData[$key] = $arrRow['name'];
				
			$arrSub['attri'] 	= $arrData;
			
			$attributes[$attribute['name']] = $arrSub;
			
			$sortsValue[] = $attribute['sorts'];
			
			
			
		}
		
	}
}

array_multisort($sortsValue, $attributes);


/*----- out html -----*/
$smarty = new Smarty();	//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = CACHE_SYS_DIR;

$smarty->assign('error_message', $error_message);
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);

$smarty->assign('backurl',$backurl);

$smarty->assign('arrInfo', $arrInfo);

$smarty->assign('arrAllImage', $arrAllImage);

$smarty->assign('attributes', $attributes);

$smarty->display('listProduct_View.htm');
?>
